package mamma.dao;

import java.util.List;

import mamma.common.MybatisConfig;
import mamma.vo.FriendUpdateVo;
import mamma.vo.SearchVo;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class FriendsDao {
	private SqlSessionFactory factory;
	
	public FriendsDao(){
		factory = MybatisConfig.getSqlSessionFactory();
	}
	
	// search members detail
	public List<SearchVo> searchMemberDetail(SearchVo vo){
		SqlSession session = factory.openSession();
		List<SearchVo> list = session.selectList("friends.searchMemberDetail", vo);
		session.close();
		
		return list;
	}
	
	// the requests that the user has.
	public List<FriendUpdateVo> selectFriendsAlerts(String email){
		SqlSession session = factory.openSession();
		List<FriendUpdateVo> list = session.selectList("friends.selectFriendsAlerts", email);
		session.close();
		
		return list;
	}
	
	// the requests that I send.
	public List<FriendUpdateVo> selectMyRequest(String email){
		SqlSession session = factory.openSession();
		List<FriendUpdateVo> list = session.selectList("friends.selectMyRequest", email);
		session.close();
		
		return list;
	}
	
	// allow the friend request.
	public int friendAllow(FriendUpdateVo vo){
		SqlSession session = factory.openSession();
		int result = session.update("friends.friendAllow", vo);
		session.commit();
		session.close();
		
		return result;
	}
	
	// send a request
	public int sendRequest(FriendUpdateVo vo){
		SqlSession session = factory.openSession();
		int result = session.insert("friends.sendRequest", vo);
		session.commit();
		session.close();
		
		return result;
	}
	
	public int cancelRequest(FriendUpdateVo vo){
		SqlSession session = factory.openSession();
		int result = session.delete("friends.cancelRequest", vo);
		session.commit();
		session.close();
		
		return result;
	}
	
	// check it if the user send a request
	public FriendUpdateVo checkRequest(FriendUpdateVo vo){
		SqlSession session = factory.openSession();
		FriendUpdateVo temp = session.selectOne("friends.checkRequest", vo);
		session.close();
		
		return temp;
	}
	
	public List<FriendUpdateVo> goFriendPage(String email){
		SqlSession session = factory.openSession();
		List<FriendUpdateVo> list = session.selectList("friends.goFriendPage", email);
		session.close();
		
		return list; 
	}
}

